drop _all
capture clear matrix 
set more off
set virtual on
set memory 5g
set matsize 2000
set logtype text
capture log close
log using SES-all.do, replace

use world_child3

** keep only children with full exposure

tab infant_exp
keep if infant_exp==1

**************************

/*
1. run eq 2 using LPM and country-year dummies for all gradients

*/

**************************

* drop immunization data
drop *measles* *dpt* *merge

* merge in immunization data

sort country yearc
merge country yearc using immunization.dta

tab _merge
tab country if _merge==3

* rescale imm vars
sum lead*

gen dpt100=lead_dpt/100 /* have now dropped the 'lead' part of the name */
gen measles100=lead_measles/100
sum *100

* drop Namibia for imm rates
replace dpt100=. if country=="Namibia"
replace measles100=. if country=="Namibia"

* keep relevant years
replace dpt100=. if yearc<1984
replace measles100=. if yearc<1984

tab yearc if dpt100<.

rename tall1 T1
rename tall2 T2
rename tallhalf Th
rename shorthalf Sh
rename short1 S1
rename short2 S2

tab educf
tab educf, nolabel

gen educ_urban=(educf4==1 & urban==1 | educf3==1 & urban==1)
replace educ_urban=. if educf==. | urban==.

global controls malec urban christian muslim otherrel cbirthmth2-cbirthmth12 chld2-chld3 chldm4 age915 age1618 age2530 age3149 educm2-educm4 educf2-educf4 

* controls for when we use educ of ma in years
global controls2 malec urban christian muslim otherrel cbirthmth2-cbirthmth12 chld2-chld3 chldm4 age915 age1618 age2530 age3149 educm2-educm4 

* controls for when we use educ of fa in years
global controls3 malec urban christian muslim otherrel cbirthmth2-cbirthmth12 chld2-chld3 chldm4 age915 age1618 age2530 age3149 educf2-educf4 

keep caseid2 countryid yearc $controls height100 infant dpt100 measles100 lgdp educfyrs educmyrs educ_urban educfyrsc Th* T1* T2* Sh* S1* S2*

foreach var of varlist T1 T2 S1 S2 Th Sh height100 {
	gen `var'_educfyrsc=`var'*educfyrsc			
	gen `var'_educfyrs=`var'*educfyrs
	gen `var'_educmyrs=`var'*educmyrs
	gen `var'_educ_urban=`var'*educ_urban
	gen `var'_dpt100=`var'*dpt100
	gen `var'_measles100=`var'*measles100
	gen `var'_lgdp=`var'*lgdp
	}
	
compress

egen manum=group(caseid2)
xtset manum

**************************************************************
***		HEIGHT IN METRES
**************************************************************

// Country level SES
* GDP
xi: reg infant height100 lgdp height100_lgdp educf2-educf4 $controls i.countryid*i.yearc, cluster(countryid)

* Mother's average education
xi: reg infant height100 educfyrsc height100_educfyrsc $controls i.countryid*i.yearc, cluster(countryid)

* DPT immunizations rates
xi: reg infant height100 dpt100 height100_dpt100 educf2-educf4  $controls i.countryid*i.yearc, cluster(countryid)

* Measles immunization rates 
xi: reg infant height100 measles100 height100_measles100 educf2-educf4  $controls i.countryid*i.yearc, cluster(countryid)

// Individual (parental) level SES
* Mother's education in years
xi: reg infant height100 educfyrs height100_educfyrs educf2-educf4  $controls2 i.countryid*i.yearc, cluster(countryid)

* Mother is educated and urban
xi: reg infant height100 educ_urban height100_educ_urban educf2-educf4  $controls i.countryid*i.yearc, cluster(countryid)

* Father's education
xi: reg infant height100 educmyrs height100_educmyrs educf2-educf4  $controls3 i.countryid*i.yearc, cluster(countryid)

**************************************************************
***		ASYMMETRY
**************************************************************

// Country level SES

* GDP
xi: reg infant Th T1 T2 Sh S1 S2 lgdp Th_lgdp T1_lgdp T2_lgdp Sh_lgdp S1_lgdp S2_lgdp educf2-educf4 $controls i.countryid*i.yearc, cluster(countryid)

* Mother's average education
xi: reg infant Th T1 T2 Sh S1 S2 educfyrsc Th_educfyrsc T1_educfyrsc T2_educfyrsc Sh_educfyrsc S1_educfyrsc S2_educfyrsc $controls i.countryid*i.yearc, cluster(countryid)

* DPT immunization rates
xi: reg infant Th T1 T2 Sh S1 S2 dpt100 Th_dpt100 T1_dpt100 T2_dpt100 Sh_dpt100 S1_dpt100 S2_dpt100 educf2-educf4  $controls i.countryid*i.yearc, cluster(countryid)

* Measles immunization rates
xi: reg infant Th T1 T2 Sh S1 S2 measles100 Th_measles100 T1_measles100 T2_measles100 Sh_measles100 S1_measles100 S2_measles100 educf2-educf4  $controls i.countryid*i.yearc, cluster(countryid)

// Individual (parental) level SES

* Mother's education
xi: reg infant Th T1 T2 Sh S1 S2 educfyrs Th_educfyrs T1_educfyrs T2_educfyrs Sh_educfyrs S1_educfyrs S2_educfyrs educf2-educf4  $controls i.countryid*i.yearc, cluster(countryid)

* Mother is educated and urban 
xi: reg infant Th T1 T2 Sh S1 S2 educ_urban Th_educ_urban T1_educ_urban T2_educ_urban Sh_educ_urban S1_educ_urban S2_educ_urban educf2-educf4  $controls i.countryid*i.yearc, cluster(countryid)

* Father's education
xi: reg infant Th T1 T2 Sh S1 S2 educmyrs Th_educmyrs T1_educmyrs T2_educmyrs Sh_educmyrs S1_educmyrs S2_educmyrs educf2-educf4  $controls i.countryid*i.yearc, cluster(countryid)

log close
exit